﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Q1;
using System.Security.Cryptography;

namespace patcher
{
    class Program
    {
        static string GetMd5Hash(MD5 md5Hash, byte[] input)
        {

            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(input);

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }

        static string GetSHA1Hash(SHA1 sha1, byte[] input)
        {
            byte[] data = sha1.ComputeHash(input);

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        static void Main(string[] args)
        {
            MD5 md5 = MD5.Create();
            SHA1 sha1 = SHA1.Create();
           if (args.Length > 0)
            {
                foreach (string arg in args)
                {
                    if (File.Exists(arg))
                    {
                        string hash = GetMd5Hash(md5, File.ReadAllBytes(arg));
                        string hash2 = GetSHA1Hash(sha1, File.ReadAllBytes(arg));

                        File.WriteAllText("Checksums", "MD5: " + hash + Environment.NewLine + "SHA1: " + hash2);
                    }
                }
            }

            /*
             * <file>
             * <name></name>
             * <md5></md5>
             * <sha1></sha1>
             * <download></download>
             * </file>
             */

            string[] d = Directory.GetFiles(Environment.CurrentDirectory);
           
            StringBuilder sb = new StringBuilder();
            
            foreach (string f in d)
            {
                sb.AppendLine("<file>");
                string hash = GetMd5Hash(md5, File.ReadAllBytes(f)); 
                string hash2 = GetSHA1Hash(sha1, File.ReadAllBytes(f));
                sb.AppendLine("<name>" + Path.GetFileName(f) + "</name>");
                
                sb.AppendLine("<md5>" + hash + "</md5>");
                sb.AppendLine("<sha1>" + hash2 + "</sha1>");
                sb.AppendLine("<download></download>");
                sb.AppendLine("</file>");
            }

            File.WriteAllText("cache.txt", sb.ToString());
        }
    }
}
